<template>
  <div class="pos">
    <el-row>
      <el-col :span="7" class="post-order" id="order-list">
        <el-tabs>
          <el-tab-pane label="点餐">
            <el-table :data="tableData" border style="width: 100%;">
              <el-table-column prop="goodsName" label="商品名称"></el-table-column>
              <el-table-column prop="count" label="数量" width="50"></el-table-column>
              <el-table-column prop="price" label="金额" width="70"></el-table-column>
              <el-table-column label="操作" width="100" fixed="right">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="delSingleGoods(scope.row)">删除</el-button>
                  <el-button type="text" size="small" @click="addOrderList(scope.row)">增加</el-button>
                </template>  
              </el-table-column>
              
            </el-table>
            <div class="total-box">
              <small>数量：</small>{{totalCount}}   <small>金额总计：</small>{{totalMoney}}元
            </div>
            <div class="div-btn">
              <el-button type="warning">挂单</el-button>
              <el-button type="danger" @click="delAllgoods">清空</el-button>
              <el-button type="primary" @click="checkout">结账</el-button>              
            </div>
          </el-tab-pane>
          <el-tab-pane label="挂单">
            挂单
          </el-tab-pane>
          <el-tab-pane label="外卖">
            外卖
          </el-tab-pane>          
        </el-tabs>
      </el-col>
      <el-col :span="17">
        <!-- 常用商品 -->
        <div class="often-goods">
          <h2 class="often-goods-title">常用商品</h2>
          <div class="often-goods-list">
            <ul>
              <li v-for="item in oftenGoods" :key="item.id" @click="addOrderList(item)">
                <span>{{item.goodsName}}</span>
                <span class="o-price">￥{{item.price}}元</span>
              </li>
            </ul>
          </div>
        </div>
        
        <div class="goods-type">
          <el-tabs>
            <el-tab-pane label="汉堡">
              <div>
                <ul class="cookList">
                  <li v-for="goods in type0Goods" :key="goods.id" @click="addOrderList(goods)">
                    <span class="foodImg"><img :src="goods.goodsImg"></span>
                    <span class="foodName">{{goods.goodsName}}</span>
                    <span class="foodPrice">￥{{goods.price}}元</span>
                  </li>
                </ul>
              </div>
            </el-tab-pane>
            <el-tab-pane label="小食">
              <div>
                <ul class="cookList">
                  <li v-for="goods in type1Goods" :key="goods.id" @click="addOrderList(goods)">
                    <span class="foodImg"><img :src="goods.goodsImg"></span>
                    <span class="foodName">{{goods.goodsName}}</span>
                    <span class="foodPrice">￥{{goods.price}}元</span>
                  </li>
                </ul>
              </div>
            </el-tab-pane>
            <el-tab-pane label="饮料">
              <div>
                <ul class="cookList">
                  <li v-for="goods in type2Goods" :key="goods.id" @click="addOrderList(goods)">
                    <span class="foodImg"><img :src="goods.goodsImg"></span>
                    <span class="foodName">{{goods.goodsName}}</span>
                    <span class="foodPrice">￥{{goods.price}}元</span>
                  </li>
                </ul>
              </div>
            </el-tab-pane>
            <el-tab-pane label="套餐">
              <div>
                <ul class="cookList">
                  <li v-for="goods in type3Goods" :key="goods.id" @click="addOrderList(goods)">
                    <span class="foodImg"><img :src="goods.goodsImg"></span>
                    <span class="foodName">{{goods.goodsName}}</span>
                    <span class="foodPrice">￥{{goods.price}}元</span>
                  </li>
                </ul>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "pos",
  data() {
    return {
      tableData: [],
      oftenGoods: [],
      type0Goods: [],
      type1Goods: [],
      type2Goods: [],
      type3Goods: [],
      totalMoney: 0,
      totalCount: 0
    };
  },
  created: function() {
    axios
      .get("http://jspang.com/DemoApi/oftenGoods.php")
      .then(response => {
        this.oftenGoods = response.data;
      })
      .catch(error => {
        console.log(error);
      });

    axios
      .get("http://jspang.com/DemoApi/typeGoods.php")
      .then(response => {
        this.type0Goods = response.data[0];
        this.type1Goods = response.data[1];
        this.type2Goods = response.data[2];
        this.type3Goods = response.data[3];
      })
      .catch(error => {
        console.log(error);
      });
  },
  mounted: function() {
    var orderHeight = document.body.clientHeight;
    document.getElementById("order-list").style.height = orderHeight + "px";
  },
  methods: {
    addOrderList(goods) {
      //商品是否已经存在订单列表中
      let isHave = false;

      for (let i = 0, len = this.tableData.length; i < len; i++) {
        if (this.tableData[i].goodsId === goods.goodsId) {
          isHave = true;
        }
      }
      // 根据判断的值编写业务逻辑
      if (isHave) {
        // 改变列表中商品的数量
        let arr = this.tableData.filter(a => a.goodsId === goods.goodsId);
        arr[0].count++;
      } else {
        let newsGoods = {
          goodsId: goods.goodsId,
          goodsName: goods.goodsName,
          price: goods.price,
          count: 1
        };
        this.tableData.push(newsGoods);
      }

      //进行数量和价格的汇总计算
      this.getAllMoney();
    },

    delSingleGoods(goods) {
      this.tableData = this.tableData.filter(o => o.goodsId != goods.goodsId);
      this.getAllMoney();
    },

    // 汇总
    getAllMoney() {
      // 汇总归零 重新计算
      this.totalCount = 0;
      this.totalMoney = 0;

      if (this.tableData) {
        //进行数量和价格的汇总计算
        this.tableData.forEach(element => {
          this.totalCount += element.count;
          this.totalMoney += element.price * element.count;
        });
      }
    },

    delAllgoods() {
      this.tableData = [];
      this.totalCount = 0;
      this.totalMoney = 0;
    },

    // 模拟结账
    checkout() {
      if(this.tableData.length != 0) {
        // axios.post('', {

        // })
        // .then()
        // .catch()

        this.tableData = [];
        this.totalCount = 0;
        this.totalMoney = 0;
        this.$message({
          message: '结账成功，感谢你又为店里出了一份力！',
          type: 'success'
        })
      }else {
        this.$message.error('未购买任何商品');
      }
    }
  }
};
</script>

<style>
.post-order {
  height: 100%;
  background-color: #f9fafc;
  border-right: 1px solid #c0ccca;
}

.el-tabs__header {
  margin: 0 !important;
}

.div-btn {
  margin-top: 10px;
}

.often-goods-title {
  height: 20px;
  padding: 10px;
  margin: 0;
  border-bottom: 1px solid #d3dce6;
  font-size: 16px;
  font-weight: normal;
  text-align: left;
  background-color: #f9fafc;
}

.often-goods-list li {
  float: left;
  border: 1px solid #f9fafc;
  padding: 10px;
  margin: 10px;
  list-style: none;
  background-color: white;
  cursor: pointer;
}

.o-price {
  color: #58b7ff;
}

.goods-type {
  clear: both;
}

.cookList li {
  list-style: none;
  width: 23%;
  border: 1px solid #e5e9f2;
  height: auot;
  overflow: hidden;
  background-color: #fff;
  padding: 2px;
  float: left;
  margin: 2px;
  cursor: pointer;
}
.cookList li span {
  display: block;
  float: left;
}
.foodImg {
  width: 40%;
}

.foodImg img {
  width: 100%;
}
.foodName {
  font-size: 18px;
  padding-left: 10px;
  color: brown;
}
.foodPrice {
  font-size: 16px;
  padding-left: 10px;
  padding-top: 10px;
}

.total-box {
  padding: 0;
  border-bottom: 1px solid #d3dce6;
  background-color: #fff;
}
</style>
